/*
 * Copyright (C) 2017 yixun All Rights Reserved.
 * 
 * CommissionRuleParentServiceImpl.java
 */
package com.yixun.qfbao.service.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.mapper.CommissionRuleParentMapper;
import com.yixun.qfbao.model.CommissionRuleParent;
import com.yixun.qfbao.model.dto.CommissionRuleParentDto;
import com.yixun.qfbao.model.enums.CommisionEnum;
import com.yixun.qfbao.model.enums.CommisionStateEnum;
import com.yixun.qfbao.service.CommissionRuleParentService;
import com.yixun.qfbao.util.RuleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;


/**
 * [文件名称]<br>
 * CommissionRuleParentServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 佣金规则公共表 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2017-10-18 18:33:30 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("commissionRuleParentService")
public class CommissionRuleParentServiceImpl implements CommissionRuleParentService {
	
	@Autowired
	private CommissionRuleParentMapper mapper;
	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<CommissionRuleParent> getPageModel(CommissionRuleParentDto entity,int begin,int end){
		Integer total = mapper.count(entity);
		List<CommissionRuleParent> rows = mapper.list(entity,begin,end);
	    Page<CommissionRuleParent> msr = new Page<CommissionRuleParent>(total, rows);
		return msr;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<CommissionRuleParent> selectListByDto(CommissionRuleParentDto entity){
		return mapper.selectListByDto(entity);
	}
	
	/**
	 * 查询单个
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public CommissionRuleParent selectByDto(CommissionRuleParentDto entity){
		return mapper.selectByDto(entity);
	}
	
	/**
	 * @param
	 * @return 分页记录
	 */
	public CommissionRuleParent selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}
	/**
	 * @param entity 新增记录的详细信息
	 * @return 新增行数
	 */
	public CommissionRuleParentDto create(CommissionRuleParentDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity 更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(CommissionRuleParentDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}
    
	/**
	 * @param entity 待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(CommissionRuleParentDto entity) {
		return mapper.del(entity);
	}
	
	/**
	 * @param entity 新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(CommissionRuleParentDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	/**
	 * @param entity 根据pojo搜索如果主键为空则返回它本身
	 * @return 一行记录或本身
	 */
	@Override
	public CommissionRuleParent selectBy(CommissionRuleParent entity) {
		if(entity!=null&&entity.getId()!=null){
			return mapper.selectById(entity.getId());
		}
		return entity;
	}

	@Override
	public List<CommissionRuleParentDto> getSetCommissionDetail(String groupId) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<CommissionRuleParentDto> list = mapper.getSetCommissionDetail(groupId);
		Calendar c = Calendar.getInstance();
		for(CommissionRuleParentDto dto:list){
			if(dto.getLostTime()!=null){
				dto.setLostDate(sdf.format(dto.getLostTime()));
			}
			if(dto.getEffectTime()!=null){
				dto.setEffectDate(sdf.format(dto.getEffectTime()));
			}
			if(!StringUtils.isEmpty(dto.getRule())&&!StringUtils.isEmpty(dto.getSort())){
				String rule = RuleUtil.getRule(dto.getRule(),dto.getSort());
				dto.setRule(rule);
			}
			if(!StringUtils.isEmpty(dto.getState())){
				dto.setStateValue(CommisionStateEnum.getByNo(dto.getState()).getDes());

				if (CommisionEnum.COMMISION_STATE_2.getCode().equals(dto.getState())) {
					if(dto.getBeginTime() != null){
						dto.setBeginDate(sdf.format(dto.getBeginTime()));
					}
					if(dto.getEndTime() != null){
						c.setTime(dto.getEndTime());
						c.add(Calendar.DAY_OF_YEAR,-1);
						dto.setEndDate(sdf.format(c.getTime()));
					}
				}else {
					dto.setBeginDate(sdf.format(dto.getEffectTime()));
					if (!StringUtils.isEmpty(dto.getLostTime())) {
						dto.setEndDate(sdf.format(dto.getLostTime()));
					}
				}
			}

		}
		return list;
	}
	@Override
	public CommissionRuleParentDto getCommissionRule(Long id){
		CommissionRuleParentDto dto = new CommissionRuleParentDto();
		dto.setId(id);
		return  mapper.selectCommissionRuleParentByDto(dto);
	}
}
